﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wcsServo.dao;
using wcsServo.domain;
using wcsServo.entity;

namespace wcsServo.service
{
    public class StnMappingService
    {
        public ResultData_FUNC UpdateStation(string stnNo, string status)
        {
            ResultData_FUNC resultData = new ResultData_FUNC
            {
                Code = "0",
                Msg = $"站点[{stnNo}]状态[{status}]更新成功!",
                Writes = ""
            };

            try
            {
                //根据stnNo判断站点是否存在
                string stnSql = $"select * from wcs_stn_mapping where wcs_station='{stnNo}' limit 1";
                StnMapEntity stnEntity = DBFactory.QueryFirst<StnMapEntity>(stnSql);
                if (stnEntity == null)
                {
                    resultData.Code = "update_stn_error_01";
                    resultData.Msg = $"货位[{stnNo}]未在[wcs_stn_mapping]进行配置,请核查!";
                    return resultData;
                }

                if ("1".Equals(status))
                {
                    //试用年月日代替托盘码
                    stnEntity.CONT_NO = stnNo + "#" + DateTime.Now.ToString("yyyyMMddHHmmss");
                    stnEntity.STATUS = 2;
                }
                else
                {
                    stnEntity.CONT_NO = "";
                    stnEntity.STATUS = 0;
                }

                stnEntity.UPDATE_DATE = DateTime.Now;
                DBFactory.UpdateStnMappingById(stnEntity);

                return resultData;
            }
            catch (Exception ex)
            {
                resultData.Code = "cancel_stn_update_500";
                resultData.Msg = $"站点{stnNo}状态更新失败!causeBy:{ex.Message}";
                return resultData;
            }
        }
    }
}
